package service.Impl;

import dao.UserDao;
import dao.impl.UserDaoImpl;
import entity.User;
import service.UserService;

import java.util.List;

public class UserServiceImpl implements UserService {
    UserDao UserDao=new UserDaoImpl();
    public boolean customerLogin(Integer id, String passWord) {
        User user=UserDao.selectUserById(id);
        if (user!=null&&user.getLevel()==1&&user.getId().equals(id)&&user.getPassWord().equals(passWord)){
            System.out.println("登录成功！");
            return true;
        }
        else {
            System.out.println("登录失败，id不存在或密码输入错误！");
            return false;
        }
    }
    public boolean sellerLogin(Integer id, String passWord){
        User user=UserDao.selectUserById(id);
        if (user!=null&&user.getLevel()==2&&user.getId().equals(id)&&user.getPassWord().equals(passWord)){
            System.out.println("登录成功！");
            return true;
        }
        else {
            System.out.println("登录失败，id不存在或密码输入错误！");
            return false;
        }
    }

    @Override
    public boolean administratorLogin(Integer id, String passWord) {
        User user=UserDao.selectUserById(id);
        if (user!=null&&user.getLevel()==3&&user.getId().equals(id)&&user.getPassWord().equals(passWord)){
            System.out.println("登录成功！");
            return true;
        }
        else {
            System.out.println("登录失败，id不存在或密码输入错误！");
            return false;
        }
    }

    @Override
    public boolean registerUser(User user) {
        User user1=UserDao.selectUserById(user.getId());
        if (user1==null){
            UserDao.insertUser(user);
            System.out.println("恭喜注册成功！");
            return true;
        } else{
            System.out.println("用户已存在,注册失败！");
            return false;
        }
    }

    @Override
    public boolean insertUser(User user) {
        User user1=UserDao.selectUserById(user.getId());
        if (user1==null){
            UserDao.insertUser(user);
            System.out.println("添加成功！");
            return true;
        }else {
            System.out.println("用户已存在，添加失败！");
            return false;
        }
    }

    @Override
    public boolean deleteUserById(Integer id) {
        User user=UserDao.selectUserById(id);
        if (user!=null){
            UserDao.deleteUserById(id);
            System.out.println("删除成功！");
            return true;
        }else {
            System.out.println("用户不存在，删除失败！");
            return false;
        }
    }

    @Override
    public boolean updateUserById(Integer id,User user) {
        User user1=UserDao.selectUserById(id);
        if (user1!=null){
            UserDao.updateUserById(id,user);
            System.out.println("更改成功！");
            return true;
        }else {
            System.out.println("用户不存在，更改失败！");
            return false;
        }
    }

    @Override
    public User selectUserById(Integer id) {
        User user=UserDao.selectUserById(id);
        if (user!=null){
            return user;
        }else {
            return null;
        }
    }

    @Override
    public User selectUserByUserName(String userName) {
        User user=UserDao.selectUserByUserName(userName);
        if (user!=null){
            return user;
        }else {
            System.out.println("用户不存在，查询失败！");
            return null;
        }
    }

    @Override
    public List<User> selectAllUser() {
        List<User> userList=UserDao.selectAllUser();
        if (userList!=null){
            return userList;
        }else{
            System.out.println("无用户！");
            return null;
        }
    }
}
